package com.java.comprehensive.arithmetic.programmers;

/**
 * @Author: Dgg
 * @Date: 2022/5/1 13:23
 */
public class OneEditAway {

	public static boolean oneEditAway(String first, String second) {
		int l = first.length();
		int r = second.length();
		int abs = l - r;
		int temp = 0;
		if (Math.abs(abs) > 1) {
			return false;
		} else if (abs == 0) {
			for (int i = 0; i < l; i++) {
				if (first.charAt(i) != second.charAt(i)) {
					temp += 1;
				}
			}
			if (temp > 1) {
				return false;
			}
		} else {
			if (first.length() != 0 && second.length() != 0) {
				int i = 0;
				int j = 0;
				while (temp <= 1) {
					if (first.charAt(i) == second.charAt(j)) {
						i++;
						j++;
					} else {
						temp++;
						if (l > r) {
							i++;
						} else {
							j++;
						}
					}
					if ((i >= l || j >= r) && temp <= 1) {
						return true;
					}
				}
				return false;
			} else {
				return true;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		String first = "mart";
		String second = "karma";
		System.out.println(oneEditAway(first, second));

	}
}
